package cn.ac.iscas.algorithm;

import java.util.Map;
import java.util.Set;

/**
 * @author peterstone
 * 
 */
public class Similarity {
	public double getSimilariy(Map<String, Double> queryVector,
			Map<String, Double> docVector) {
		double result = 0.0;
		double querylength = 0.0;
		double doclength = 0.0;
		try {
			Set<String> queryKeywords = queryVector.keySet();
			for (String s : queryKeywords) {
				querylength += queryVector.get(s) * queryVector.get(s);
				if (docVector.containsKey(s)) {
					result += queryVector.get(s) * docVector.get(s);
				}
			}
			querylength = Math.sqrt(querylength);
			
			Set<String> docKeywords = docVector.keySet();
			for (String s : docKeywords) {
				doclength += docVector.get(s) * docVector.get(s);
			}
			doclength = Math.sqrt(doclength);
			result /= (querylength * doclength); 
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
}
